import 'package:shared_preferences/shared_preferences.dart';

class SpUtil {
  static SharedPreferences? _prefs;

  static Future<void> init() async {
    _prefs ??= await SharedPreferences.getInstance();
  }

  static Future<bool> putString(String key, String value) async {
    await init();
    return _prefs!.setString(key, value);
  }

  static String? getString(String key, {String? defValue}) {
    return _prefs?.getString(key) ?? defValue;
  }

  static Future<bool> putInt(String key, int value) async {
    await init();
    return _prefs!.setInt(key, value);
  }

  static int? getInt(String key, {int? defValue}) {
    return _prefs?.getInt(key) ?? defValue;
  }

  static Future<bool> putDouble(String key, double value) async {
    await init();
    return _prefs!.setDouble(key, value);
  }

  static double? getDouble(String key, {double? defValue}) {
    return _prefs?.getDouble(key) ?? defValue;
  }

  static Future<bool> putBool(String key, bool value) async {
    await init();
    return _prefs!.setBool(key, value);
  }

  static bool? getBool(String key, {bool? defValue}) {
    return _prefs?.getBool(key) ?? defValue;
  }

  static Future<bool> putStringList(String key, List<String> value) async {
    await init();
    return _prefs!.setStringList(key, value);
  }

  static List<String>? getStringList(String key, {List<String>? defValue}) {
    return _prefs?.getStringList(key) ?? defValue;
  }

  static Future<bool> remove(String key) async {
    await init();
    return _prefs!.remove(key);
  }

  static Future<bool> clear() async {
    await init();
    return _prefs!.clear();
  }
}
